package com.evry.marshallingtest.support;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.evry.marshallingtest.newmodel.Customer;
import com.evry.marshallingtest.newmodel.Database;
import com.evry.marshallingtest.newmodel.DeliveryType;
import com.evry.marshallingtest.newmodel.Order;
import com.evry.marshallingtest.newmodel.Product;
import com.evry.marshallingtest.newmodel.ProductIds;
import com.evry.marshallingtest.oldmodel.Customers;
import com.evry.marshallingtest.oldmodel.Orders;

@Service("transformDataLogic")
public class TransformDataLogic {

	public Database createNewDataBaseFromOld(
			com.evry.marshallingtest.oldmodel.Database oldDatabase) {
		Database database = new Database();
		database.getCustomers().setCustomer(
				transformCustomers(oldDatabase.getCustomers()));
		database.getProducts().setProduct(
				transformProducts(oldDatabase.getProducts()));
		database.getOrders()
				.setOrders(transformOrders(oldDatabase.getOrders()));
		return database;
	}

	private List<Product> transformProducts(
			com.evry.marshallingtest.oldmodel.Products oldProducts) {
		List<Product> transformedProducts = new ArrayList<Product>();
		for (com.evry.marshallingtest.oldmodel.Product oldProduct : oldProducts
				.getProduct()) {
			Product product = new Product();
			product.setProductId(oldProduct.getProductId());
			product.setDesc(oldProduct.getDesc());
			product.setPrice(oldProduct.getPrice());
			transformedProducts.add(product);
		}

		Product product1 = new Product();
		product1.setProductId("0001");
		product1.setDesc("Fishtank");
		product1.setPrice(40.00);
		transformedProducts.add(product1);

		Product product2 = new Product();
		product2.setProductId("0002");
		product2.setDesc("Armchair");
		product2.setPrice(55.00);
		transformedProducts.add(product2);

		Product product3 = new Product();
		product3.setProductId("0003");
		product3.setDesc("Bird");
		product3.setPrice(23.00);
		transformedProducts.add(product3);
		return transformedProducts;
	}

	private List<Order> transformOrders(Orders oldOrders) {
		List<Order> transormedOrders = new ArrayList<Order>();
		for (com.evry.marshallingtest.oldmodel.Order oldOrder : oldOrders
				.getOrder()) {
			Order order = new Order();
			order.setOrderNumber(oldOrder.getOrderNumber());
			order.setPersonalCodeNumber(oldOrder.getCustomer()
					.getPersonalCodeNumber());
			order.setDeliveryType(mapOldDeliveryType(oldOrder.getDeliveryType()));
			

			ProductIds productIds = new ProductIds();
			for(com.evry.marshallingtest.oldmodel.Product product : oldOrder.getItems().getProduct()){
				productIds.getId().add(product.getProductId());
			}
			order.setProductIds(productIds);
			
			transormedOrders.add(order);
		}
		return transormedOrders;
	}

	private DeliveryType mapOldDeliveryType(
			com.evry.marshallingtest.oldmodel.DeliveryType deliveryType) {
		switch (deliveryType) {
		case STANDARD:
			return DeliveryType.STANDARD;
		case EXPRESS:
			return DeliveryType.EXPRESS;
		case ULTRASUPEREXPRESS:
			return DeliveryType.ULTRASUPEREXPRESS;
		default:
			return null;
		}
	}

	private List<Customer> transformCustomers(Customers oldCustomers) {
		List<Customer> transformedCustomers = new ArrayList<Customer>();
		for (com.evry.marshallingtest.oldmodel.Customer oldCustomer : oldCustomers
				.getCustomer()) {
			Customer customer = new Customer();
			customer.setFirstName(oldCustomer.getFirstName());
			customer.setLastName(oldCustomer.getLastName());
			customer.setPersonalCodeNumber(oldCustomer.getPersonalCodeNumber());
			customer.setStreetAdress(oldCustomer.getStreetAdress());
			customer.setPostalAdress(oldCustomer.getPostalAdress());
			customer.setPostalCode(oldCustomer.getPostalCode());
			transformedCustomers.add(customer);
		}
		return transformedCustomers;
	}

}
